package cn.itcast.work.springsecurity;

import cn.itcast.work.dao.user.UserMapper;
import cn.itcast.work.pojo.User;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

/**
 * * @author: Mao Wanting 2022051602008
 * * @date: 2024/12/23
 * *description:根据用户名查找用户信息
 **/
@Slf4j
@Service
public class SecurityService implements UserDetailsService {

    @Autowired
    private UserMapper userMapper;


    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        log.info("[service]:执行loadUserByUsername");
        //返回查找出来的用户，以后将用户输入的密码和数据库查出来的密码做匹配
        User user = userMapper.selectUserByPhone(username);
        if (user == null) {
            throw new UsernameNotFoundException("user not found!");
        }
        return new CustomUserDetails(user);

    }
}
